package com.ratta.supernote.note.plugincore.modules;

import android.graphics.Rect;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.ratta.supernote.note.plugincore.NativeNoteTrailsSpec;
import com.ratta.supernote.note.plugincore.api.NoteDataCacheApi;
import com.ratta.supernote.note.plugincore.api.NoteLibAPI;
import com.ratta.supernote.note.plugincore.bean.NoteOperateTrails;
import com.ratta.supernote.note.plugincore.bean.TrailsData;
import com.ratta.supernote.note.plugincore.utils.FileUtils;
import com.ratta.supernote.note.plugincore.utils.UiHandler;

import java.io.File;

/**
 * Operations for note strokes
 */
public class NoteTrailsModule extends NativeNoteTrailsSpec {

    private final String TAG = NAME;

    public NoteTrailsModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public void saveSticker(String path, Promise promise) {
        Log.i(TAG, "NoteTrailsModule saveSticker");
        TrailsData trailsData = NoteDataCacheApi.getInstance().getTrailsData().getValue();
        if (trailsData == null || trailsData.noteOperateTrails == null) {
            promise.reject(new Throwable("No Trails Data"));
            return;
        }
        NoteOperateTrails noteOperateTrails = trailsData.noteOperateTrails;
        promise.resolve(NoteLibAPI.getInstance().saveSticker(noteOperateTrails, path));

    }

    @Override
    public void getStickerSize(String path, Promise promise) {
        if (!FileUtils.isFileExists(path)) {
            promise.reject(new Throwable("File is not Exists"));
            return;
        }
        Rect rect = NoteLibAPI.getInstance().getStickerRect(path);
        WritableMap stickerSize = Arguments.createMap();
        stickerSize.putInt("width", rect.width());
        stickerSize.putInt("height", rect.height());
        promise.resolve(stickerSize);


    }

    @Override
    public void generateStickerThumbnail(String stickerPath, String thumbnailPath, double width, double height, Promise promise) {
        promise.resolve(NoteLibAPI.getInstance().generateStickerThumbnail(stickerPath, thumbnailPath,
                (int) width, (int) height));
    }

    @Override
    public void convertImage2Sticker(String pngPath, String stickerPath, Promise promise) {
        promise.resolve(NoteLibAPI.getInstance().convertImage2Sticker(pngPath, stickerPath));

    }

    @Override
    public void insertSticker(String path, Promise promise) {
        Log.i(TAG, "NoteTrailsModule insertSticker");
        if (!FileUtils.isFileExists(path)) {
            promise.reject(new Throwable("Sticker is not Exists"));
            return;
        }

        TrailsData trailsData = new TrailsData();
        trailsData.pasteStickerPath = path;
        NoteDataCacheApi.getInstance().getTrailsData().postValue(trailsData);
        promise.resolve(true);
    }
}
